{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Check the Environment was Created Correctly"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import boto3\n",
    "\n",
    "region = boto3.Session().region_name\n",
    "session = boto3.session.Session()\n",
    "\n",
    "ec2 = boto3.Session().client(service_name=\"ec2\", region_name=region)\n",
    "sm = boto3.Session().client(service_name=\"sagemaker\", region_name=region)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Check Environment"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "\n",
    "notebook_instance_name = None\n",
    "\n",
    "try:\n",
    "    with open(\"/opt/ml/metadata/resource-metadata.json\") as notebook_info:\n",
    "        data = json.load(notebook_info)\n",
    "        domain_id = data[\"DomainId\"]\n",
    "        resource_arn = data[\"ResourceArn\"]\n",
    "        region = resource_arn.split(\":\")[3]\n",
    "        name = data[\"ResourceName\"]\n",
    "    print(\"DomainId: {}\".format(domain_id))\n",
    "    print(\"Name: {}\".format(name))\n",
    "except:\n",
    "    print(\"+++++++++++++++++++++++++++++++++++++++++\")\n",
    "    print(\"[ERROR]: COULD NOT RETRIEVE THE METADATA.\")\n",
    "    print(\"+++++++++++++++++++++++++++++++++++++++++\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "describe_domain_response = sm.describe_domain(DomainId=domain_id)\n",
    "print(describe_domain_response[\"Status\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "try:\n",
    "    get_status_response = sm.get_sagemaker_servicecatalog_portfolio_status()\n",
    "    print(get_status_response[\"Status\"])\n",
    "except:\n",
    "    pass"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Summary: Check All Required Settings Are Set Correctly"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if (\n",
    "    describe_domain_response[\"Status\"] == \"InService\"\n",
    "    and get_status_response[\"Status\"] == \"Enabled\"\n",
    "    and \"datascience\" in name\n",
    "):\n",
    "    setup_instance_check_passed = True\n",
    "    print(\"[OK] Checks passed!  Great Job!!  Please Continue.\")\n",
    "else:\n",
    "    setup_instance_check_passed = False\n",
    "    print(\"+++++++++++++++++++++++++++++++++++++++++++++++\")\n",
    "    print(\"[ERROR]: WE HAVE IDENTIFIED A MISCONFIGURATION.\")\n",
    "    print(describe_domain_response[\"Status\"])\n",
    "    print(get_status_response[\"Status\"])\n",
    "    print(name)\n",
    "    print(\"+++++++++++++++++++++++++++++++++++++++++++++++\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you see errors ^^ above ^^, make sure you have enabled \"Projects\" in SageMaker Studio and RE-RUN THIS NOTEBOOK.\n",
    "\n",
    "![](img/check_projects_enabled.png)\n",
    "\n",
    "![](img/enable_projects.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Do not move forward if you see an ERROR message ^^ above ^^"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(setup_instance_check_passed)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%store setup_instance_check_passed"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%store"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Release Resources"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%html\n",
    "\n",
    "<p><b>Shutting down your kernel for this notebook to release resources.</b></p>\n",
    "<button class=\"sm-command-button\" data-commandlinker-command=\"kernelmenu:shutdown\" style=\"display:none;\">Shutdown Kernel</button>\n",
    "        \n",
    "<script>\n",
    "try {\n",
    "    els = document.getElementsByClassName(\"sm-command-button\");\n",
    "    els[0].click();\n",
    "}\n",
    "catch(err) {\n",
    "    // NoOp\n",
    "}    \n",
    "</script>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%javascript\n",
    "\n",
    "try {\n",
    "    Jupyter.notebook.save_checkpoint();\n",
    "    Jupyter.notebook.session.delete();\n",
    "}\n",
    "catch(err) {\n",
    "    // NoOp\n",
    "}"
   ]
  }
 ],
 "metadata": {
  "instance_type": "ml.t3.medium",
  "kernelspec": {
   "display_name": "Python 3 (Data Science)",
   "language": "python",
   "name": "python3__SAGEMAKER_INTERNAL__arn:aws:sagemaker:us-east-1:081325390199:image/datascience-1.0"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
